package littlepicture.control.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import littlepicture.control.delegates.ProjectDelegate;
	import littlepicture.model.EmployeeModel;
	import littlepicture.model.ModelLocator;
	import littlepicture.model.RecordModel;
	import littlepicture.pojo.DailyRecords;
	import littlepicture.pojo.LpProject;
	import littlepicture.pojo.Module;
	import littlepicture.pojo.Person;
	import littlepicture.pojo.Record;
	import littlepicture.utils.RecordUtil;
	import littlepicture.views.Cell;
	import littlepicture.views.DateTile;
	import littlepicture.views.ModuleTable;
	import littlepicture.views.NameField;
	import littlepicture.views.PlusRecord;
	
	import mx.containers.Grid;
	import mx.containers.GridItem;
	import mx.containers.GridRow;
	import mx.containers.VBox;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class LoadProjectCommand implements ICommand
	{
		private var _model:ModelLocator=ModelLocator.getInstance();
		
		private var startDate:Date;
		
		public function execute(event:CairngormEvent):void
		{
			var responder:Responder=new Responder(resultHandler,faultResult);
			var delegate:ProjectDelegate=ProjectDelegate.getInstance(responder);	
			startDate = event.data as Date;
			//load little picture
			delegate.loadProject(startDate);		
		}
		
		private function resultHandler(event:ResultEvent):void{
   			_model.project = event.result as LpProject;
   			generateLittlePicture(_model.project);
   		}
   		
   		private function faultResult(event:FaultEvent):void{
   			trace(event.fault);	
   		} 
   		
   		private function generateLittlePicture(project:LpProject):void{
   			//set date tile
			var dateTile:DateTile = _model.systemManager.document.dateTile;
			dateTile.setStartDate(startDate);
			
			//generate main ui
   		    var mainBody:VBox = _model.systemManager.document.bodyPanel;
   		    
   		    while(mainBody.getChildren().length > 2){
   		    	mainBody.removeChildAt(2);	
   		    }
   		    
   		    for each(var module:Module in project.modules){
   		    	var moduleTable:ModuleTable = new ModuleTable();
   		    	var grid:Grid = new Grid();
   		    	grid.percentWidth = 100;
				moduleTable.addChild(grid);
   		    	for each(var person:Person in module.persons){
   		    		var personRow:GridRow = new GridRow();
   		    		var nameField:NameField = new NameField();
   		    		nameField.personName = person.name;
   		    		personRow.addChild(nameField);
   		    		for each(var dailyRecords:DailyRecords in person.weeklyRecords){
   		    			var cellContainer:GridItem = new GridItem();
   		    			cellContainer.width = 150;
   		    			var cell:Cell = new Cell();
   		    			var employee:EmployeeModel = new EmployeeModel();
   		    			employee.employeeId = person.employeeId;
   		    			employee.employeeName = person.name;
   		    			cell.employee = employee;
	    				cell.date = dailyRecords.date;
   		    			for each(var record:Record in dailyRecords.records){
   		    				var recordModel:RecordModel = new RecordModel();
   		    				recordModel.content = record.content;
   		    				recordModel.recordId = record.recordId;
   		    				recordModel.status = record.status;
   		    				recordModel.employee = employee;
   		    			   	cell.addChild(RecordUtil.createRecordText(recordModel));
   		    			}
   		    			if(cell.getChildren().length >0){
   		    				var plusRecord:PlusRecord = new PlusRecord();
   		    				plusRecord.employee = employee;
   		    				cell.addChild(plusRecord);
   		    			}
   		    			cellContainer.addChild(cell);
   		    			personRow.addChild(cellContainer);
   		    		}
   		    		grid.addChild(personRow);
   		    	}
   		    	moduleTable.title = module.name;
   		    	mainBody.addChild(moduleTable);
   		    }
   		}
	}
}